<template>
  <div id="sign" v-if="show">
    <div class="modal" v-if="modalType == 0">
      <div class="modalTitle">
        <div class="quesNum">第{{ num + 1 }}/5题</div>
        <div class="quesIcon">
          <img src="../assets/imgs/jishiqi.png" alt="" />
        </div>
        <div class="quesTime">倒计时：{{ time }}s</div>
      </div>
      <progress :value="grade" max="100" class="grade"></progress>
      <div class="question">
        {{ list[num].content }}
      </div>
      <div class="answer" v-for="(item, index) in list[num].array" :key="index">
        <input
          type="radio"
          name="answer"
          id=""
          class="answerIpt"
          v-model="answerIndex"
          :value="index"
          @change="getAnswer(index)"
        />
        <label for="">
          {{ item }}
        </label>
      </div>
      <div class="sign-btn" @click="sel()">下一题</div>
    </div>
    <div class="modal success" v-else-if="modalType == 1">
      <div class="windowTop">
        <div class="windowTitle">答题结果</div>
      </div>
      <div class="modalIcon">
        <img src="../assets/imgs/laoshu.png" alt="" />
      </div>
      <div class="modalPrompt">恭喜您，答题成功！</div>
      <span class="modalText">得分超过60分，即可通过哦~</span>
      <div class="modalText">您已晋级为正式会员</div>
      <div class="sign-btn" @click="successAns">确认</div>
    </div>
    <div class="modal success" v-else-if="modalType == 2">
      <div class="windowTop">
        <div class="windowTitle">答题结果</div>
        <div class="Back" @click="goFind">跳过</div>
      </div>
      <div class="modalIcon">
        <img src="../assets/imgs/laoshu.png" alt="" />
      </div>
      <div class="modalPrompt">很抱歉，答题失败！</div>
      <div class="modalText">请返回重新答题</div>
      <div class="sign-btn" @click="again">确认</div>
      <!-- <div class="clear-btn" @click="showModal">
        <img src="../assets/imgs/cha.png" alt="" />
      </div> -->
    </div>
    <div
      class="modal interestModal selectTag"
      v-else-if="modalType == 3"
      v-loading="loading"
    >
      <div class="modalFavor">选择你喜欢的专题</div>
      <div
        class="modalTag "
        :class="item.selected ? 'InterSelected' : ''"
        v-for="(item, index) in modalList"
        :key="index"
        @click="selectInterest(item.tagId, index)"
      >
        {{ item.tagName }}-{{ item.tagContent }}
      </div>
      <div class="sign-btn interestBtn" @click="next" v-if="isBtn">保存</div>
      <div class="sign-btn interestBtn" @click="next" v-else>下一步</div>
      <!-- <div class="clear-btn" @click="showModal">
        <img src="../assets/imgs/cha.png" alt="" />
      </div> -->
    </div>
  </div>
</template>

<script>
export default {
  props: {
    fatherModal: {
      type: Number,
    },
    isFinish: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      modalType: 0,
      btnShow: true,
      show: true,
      list: [
        {
          content: "",
        },
      ],
      num: 0,
      question: [],
      grade: 0,
      time: 60,
      myTime: "",
      getIndex: -1,
      answerIndex: "",
      modalList: [],
      arr: [],
      isBtn: "",
      isFather: false,
      loading: true,
      openid: "",
    };
  },
  watch: {
    fatherModal: {
      handler(newVal, oldVal) {
        this.modalType = newVal;
      },
      immediate: true,
    },
    isFinish: {
      handler(newVal, oldVal) {
        this.isBtn = newVal;
      },
      immediate: true,
    },
  },
  created() {
   
    if (this.modalType == 0) {
      this.init();
    } else if (this.modalType === 3) {
      this.interestList();
    }
  },
  mounted() {
    this.getRem(1920, 100);
  },
  methods: {
    //主函数
    init() {
      //初始化状态
      this.num = 0;
      this.grade = 0;
      this.myTime = setInterval(this.timer, 1000);
      this.$fetch(`/laiwen/bank/frontend/list`).then((res) => {
        this.list = res.bankList;
        let answer = res.bankList;
        this.question = answer[this.num];
        for (let i of answer.keys()) {
          //数组组成题目
          let sel_arr = [];
          sel_arr.push(answer[i].aoption);
          sel_arr.push(answer[i].boption);
          sel_arr.push(answer[i].coption);
          sel_arr.push(answer[i].doption);
          this.$set(answer[i], `array`, sel_arr);
          //默认题目提示
          if (answer[i] == null) {
            this.$set(answer[i], "content", "暂无题目");
          }
        }
        this.answer = answer;
      });
    },
    //选择答案
    getAnswer(val) {
      this.getIndex = val;
    },
    goFind() {
      this.$router.push({
        path: "/",
      });
    },
    //下一题
    sel() {
      let that = this;
      //选择后才能点击按钮
      if (that.getIndex != -1) {
      } else {
        if (that.time != 0) {
          this.$message.error("亲亲请先选择答案");
          return true;
        }
      }
      //根据索引值匹配对应第几个选项
      console.log(that.getIndex);
      if (that.getIndex == that.answer[that.num].answer) {
        that.grade += 20;
      } else if (that.getIndex == -1) {
        that.$message.error("亲亲时间到了进入下一题");
      } else {
        that.$message.error("答题错误啦亲亲");
      }
      let len = that.answer.length;
      let num = that.num;
      num++;
      that.num = num;
      console.log(that.num);
      that.question = that.answer[that.num];
      //防止挂机getIndex==0,也能答对题
      that.getIndex = -1;
      that.answerIndex = "";
      that.time = 60;
      clearInterval(that.myTime);
      that.myTime = setInterval(that.timer, 1000);
      //最后一题时关闭计时器
      console.log(that.grade);
      //答完题判断成功和失败
      if (that.num == len) {
        clearInterval(that.myTime);
        if (that.grade >= 60) {
          that.modalType = 1;
        } else {
          that.modalType = 2;
        }
      }
    },
    //专题列表
    async interestList() {
      this.loading = true;
      const res = await this.$fetch(`/laiwen/interest/frontend/list`);
      this.loading = false;
      this.modalList = res.interestList;
      for (let i of this.modalList.keys()) {
        this.$set(this.modalList[i], "selected", false);
      }
      this.concernList();

      console.log(res);
    },
    //调用已关注的专题列表
    async concernList() {
      const res = await this.$fetch("/laiwen/interest/frontend/ilist", {});
      let indexArr = res.list.map((item) => item.tagId - 101);
      this.arr = res.list.map((item) => item.tagId);
      for (let i of indexArr.keys()) {
        this.modalList[indexArr[i]].selected = true;
      }
      console.log(res);
    },
    //选择专题
    selectInterest(id, index) {
      let res = [...this.modalList].find((item) => item.tagId == id);
      res.selected = !res.selected;
      if (res.selected) {
        this.arr.push(res.tagId);
      } else {
        for (let i in this.arr) {
          if (this.arr[i] == index + 1) {
            this.arr.splice(i, 1);
          }
        }
      }
      console.log(this.arr);
    },
    //选择专题后
    async next() {
      if (this.arr.length == 0) {
        this.$message.error("亲亲请先选择专题哦");
        return false;
      }
      let obj = {};
      this.$set(obj, "ids", this.arr);
      const res = await this.$post(`/laiwen/interest/frontend/save`, obj);
      // this.showModal();
      this.$router.push({
        path: "/",
      });
    },
    //成功后的操作
    successAns() {
      this.modalType = 3;
      let uesrId = JSON.parse(localStorage.getItem("user")).id;
      //openid=='' 说明为手机号登录
      if (this.openid == "") {
        localStorage.setItem("loginType", "手机号");
      } else {
        this.openid = JSON.parse(localStorage.getItem("openid"));
      }
      //获取token
      this.$post(
        `/laiwen/user/frontend/answerSave?openid=${this.openid}&userId=${uesrId}`
      ).then((res) => {
        console.log(res);
        localStorage.setItem("token", res.token);
        if (res.msg = "success") {
          console.log(res.user)
          let user = JSON.stringify(res.user)
          localStorage.setItem("user",user);
          this.interestList();
        }else{
          //处理异常
          this.loading = false;
          this.$message.error('出现未知异常请联系管理员');
        }
      });
    },
    //重试
    again() {
      this.modalType = 0;
      this.init();
      console.log(this.num);
    },
    //计时方法
    timer() {
      let that = this;
      let total_s = that.time;
      if (total_s > 0) {
        total_s--;
        that.time = total_s;
      } else {
        clearInterval(that.myTime);
        //失败逻辑代码
        that.sel();
      }
    },
  },
};
</script>

<style scoped>
img {
  width: 100%;
  height: 100%;
  flex-shrink: 0;
}
.modal .modalFavor {
  width: 1.6rem;
  height: 0.28rem;
  font-size: 0.2rem;
  font-family: PingFang SC;
  font-weight: bold;
  line-height: 0.28rem;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
  margin-bottom: 0.24rem;
}
.modal .modalTag {
  width: 2.15rem;
  height: 0.46rem;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0rem 0rem 0.05rem rgba(0, 0, 0, 0.16);
  border-radius: 0.44rem;
  margin-bottom: 0.16rem;
  font-size: 0.16rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(152, 152, 152, 1);
  opacity: 1;
  align-self: flex-start;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.gradeText{

}
.InterSelected {
  background: #fcec5b !important;
  color: #090909 !important;
}
.modal .modalTag:nth-of-type(even) {
  margin-left: 1.52rem;
}
.modal .modalTag:nth-of-type(odd) {
  margin-left: 0.69rem;
}
.modal .modalTag:nth-of-type(7) {
  margin-bottom: 0rem;
}
.selectTag {
  padding-left: 0rem !important;
  padding-right: 0rem !important;
}
.bg {
  width: 100%;
  height: 100%;
  opacity: 0.25;
  background: black;
  position: absolute;
  z-index: -1;
}
.selected {
  background: #ffd52f !important;
  color: #ffffff !important;
  border: 0.01rem solid transparent !important;
}

.modal {
  width: 4rem;
  min-height: 5rem;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  position: relative;
  flex-direction: column;
  z-index: 9999;
  background: white;
  padding: 0.32rem;
  box-sizing: border-box;
  border-radius: 0.05rem;
}
.success {
  padding-top: 0rem;
}
.modal-btns {
  width: 4.36rem;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.modalTitle {
  width: 100%;
  display: flex;
  font-size: 0.18rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
  position: relative;
  justify-content: space-between;
}
.interestBtn {
  margin-top: 0.32rem !important;
}
.quesIcon {
  width: 0.3rem;
  height: 0.3rem;
  opacity: 1;
  overflow: hidden;
  border-radius: 50%;
  position: absolute;
  right: 1.18rem;
}
.grade {
  width: 100%;
  height: 0.24rem;
  background: rgba(238, 238, 238, 1);
  opacity: 1;
  border-radius: 0.12rem;
  margin: 0.28rem 0rem;
}
.question {
  width: 100%;
  font-size: 0.2rem;
  font-family: PingFang SC;
  font-weight: bold;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
  margin-bottom: 0.4rem;
}
.answer {
  display: flex;
  font-size: 0.19rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
  align-self: flex-start;
  max-width: 4.17rem;
  margin-bottom: 0.24rem;
}
.answer:nth-of-type(6) {
  margin-bottom: 0rem;
}
label {
  font-size: 0.19rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
  margin-left: 0.16rem;
}
.finish {
  background: #999999 !important;
  color: #ffffff !important;
}

image {
  width: 100%;
  display: block;
  height: 100%;
}
.modal-text {
  height: 0.2rem;
  font-size: 0.14rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.2rem;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
}
.modal-btn {
  width: 0.88rem;
  height: 0.78rem;
  background: rgba(238, 238, 238, 1);
  opacity: 1;
  border-radius: 0.05rem;
  margin-top: 0.08rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  color: rgba(153, 153, 153, 1);
  cursor: pointer;
}
.modal-btn .gift-icon {
  width: 0.31rem;
  height: 0.31rem;
  margin-top: 0.13rem;
}
.gift-text {
  margin-top: 0.07rem;
  height: 0.2rem;
  font-size: 0.14rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.2rem;
  opacity: 1;
}
.sign-btn {
  background: #ffe300;
  opacity: 1;
  font-size: 0.16rem;
  font-family: PingFang SC;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 400;
  color: #303030;
  opacity: 1;
  cursor: pointer;
  margin-top: 0.39rem;
  width: 3.36rem;
  height: 0.42rem;
  opacity: 1;
  border-radius: 0.05rem;
}
.clear-btn {
  width: 0.24rem;
  height: 0.24rem;
  border-radius: 50%;
  opacity: 1;
  position: absolute;
  right: -0.28rem;
  top: -0.28rem;
  cursor: pointer;
}
/* 表示总长度背景色 */
progress::-webkit-progress-bar {
  background-color: #eeeeee;
  border-radius: 0.12rem;
}
progress::-webkit-progress-value {
  background: #ffd52f;
  border-radius: 0.2rem;
}

input[type="radio"] {
  width: 0.2rem;
  height: 0.2rem;
  position: relative;
  appearance: none;
  vertical-align: middle;
  outline: none !important;
}
input[type="radio"]::before {
  position: absolute;
  content: "";
  top: 50%;
  left: 0;
  margin-top: -0.1rem;
  width: 100%;
  height: 100%;
  width: 0.2rem;
  height: 0.2rem;
  background: rgba(255, 255, 255, 1);
  border: 0.01rem solid rgba(208, 208, 208, 1);
  opacity: 1;
  border-radius: 0.03rem;
}
input[type="radio"]:checked::before {
  position: absolute;
  content: "\2713";
  font-size: 0.2rem;
  font-weight: bold;
  text-align: center;
  color: white;
  background: #ffd52f;
  border: 0.01rem solid transparent;
  line-height: 0.2rem;
}
.modalIcon {
  width: 1.48rem;
  height: 1.48rem;
  opacity: 1;
  margin-top: 0.45rem;
  border-radius: 50%;
  overflow: hidden;
}
.modalPrompt {
  width: 2.34rem;
  height: 0.37rem;
  font-size: 0.26rem;
  font-family: PingFang SC;
  font-weight: bold;
  line-height: 0.37rem;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
  margin: 0.48rem 0rem 0.12rem 0rem;
}
.modalText {
  height: 0.28rem;
  font-size: 0.2rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.28rem;
  color: rgba(153, 153, 153, 1);
  opacity: 1;
}
.windowTop {
  width: 100%;
  height: 0.86rem;
  border-bottom: 0.01rem solid #eeeeee;
  position: relative;
}
.windowTitle {
  width: 0.64rem;
  height: 0.48rem;
  font-size: 0.16rem;
  font-family: PingFang SC;
  font-weight: bold;
  color: rgba(48, 48, 48, 1);
  position: absolute;
  bottom: 0;
  opacity: 1;
  border-bottom: 0.02rem solid #303030;
}
.Back {
  position: absolute;
  right: 0;
  bottom: 0;
  height: 0.48rem;
  font-family: PingFang SC;
  cursor: pointer;
  color: #6e6e6e;
  font-size: 0.16rem;
}
</style>
